package com.ffox.service.impl;

import com.ffox.entity.StudentInformation;
import com.ffox.entity.UserInformation;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.jjwt.JwtUtils;
import com.ffox.mapper.StudentLoginMapper;
import com.ffox.result.Result;
import com.ffox.service.StudentLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生登录服务实现类
 */
@Service
public class StudentLoginServiceImpl implements StudentLoginService {
    @Autowired
    private StudentLoginMapper studentLoginMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    private FastDfsUtil fastDfsUtil;

    /**
     * 学生登录方法
     *
     * @param number   学生学号
     * @param password 学生密码
     * @return 登录结果
     */
    @Override
    public Result<Map<String, Object>> studentLogin(String number, String password) {
        StudentInformation datas = studentLoginMapper.getStudentByNumber(number);
        if (datas == null) {
            return Result.fail("用户不存在");
        }
        if (!bCryptPasswordEncoder.matches(password, datas.getPassword())) {
            return Result.fail("账号或密码错误");
        }
        studentLoginMapper.updateStudent(datas.getId(), new HashMap<String, Object>() {{
            System.out.println(LocalDateTime.now());
            put("lastLoginTime", LocalDateTime.now());
        }});

        List<String> roles = Arrays.asList("ROLE_STUDENT");

        // 生成JWT令牌
        String token = JwtUtils.generateToken(number,roles);

        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("name", datas.getNickName());
        userInfo.put("avatar", datas.getAvatar());
        userInfo.put("role", "student");
        userInfo.put("number", number);

        Map<String, Object> result = new HashMap<>();
        result.put("userInfo", userInfo);
        result.put("token", token);

        return Result.success("登录成功", result);
    }

    /**
     * 学生注册方法
     *
     * @param number   学生学号
     * @param password 学生密码
     * @param realName 学生真实姓名
     * @param idCard   学生身份证号
     * @return 注册结果
     */
    @Override
    public Result<Integer> studentInsert(String number, String password, String realName, String idCard, String userNum, String userIdCard) {
        if (studentLoginMapper.getStudentByNumber(number) != null) {
            return Result.fail("用户已存在");
        }
        if(userNum != ""){
            System.out.println("userNum:"+userNum);
        UserInformation user = studentLoginMapper.getUserByNumber(userNum);
        if (user == null){
            return Result.fail("用户不存在");
        }
        String getIdCard = user.getIdCard();
        if (!getIdCard.equals(userIdCard)){
            System.out.println("表中身份证"+getIdCard);
            System.out.println("收到的"+userIdCard);
            System.out.println("身份验证失败+++++++++++++++++++++》");
            return Result.fail("监护人身份证号错误！");
        }
        }
        StudentInformation loginStudent = new StudentInformation();
        loginStudent.setNumber(number);
        loginStudent.setPassword(bCryptPasswordEncoder.encode(password));
        loginStudent.setRealName(realName);
        loginStudent.setIdCard(idCard);
        loginStudent.setUserNum(userNum);
        loginStudent.setNickName(number);
        loginStudent.setAvatar("http://192.168.190.53:80/group1/M00/00/00/wKi-NWiYwqmARlxuAAEikS43N48254.png");
        loginStudent.setCreatedAt(LocalDateTime.now());
        Integer result = studentLoginMapper.insertStudent(loginStudent);
        if (result > 0) {
            return Result.success("用户添加成功", result);
        } else {
            return Result.fail("用户添加失败");
        }
    }

    /**
     * 学生删除方法
     *
     * @param id 学生ID
     * @return 删除结果
     */
    @Override
    public Result<Integer> studentDelete(Long id) {
        Integer result = studentLoginMapper.updateStudent(id, new HashMap<String, Object>() {{
            put("status", 0);
        }});
        if (result == 1) {
            return Result.success("用户删除成功", result);
        } else {
            return Result.fail("用户删除失败");
        }
    }
}
